﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class FormMain : Form
    {
        private Game game;
        public FormMain()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 获取地图
        /// </summary>
        /// <returns></returns>
        private string GetMap()
        {
            string map;
            if (cbMap.SelectedIndex <= 0)
            {
                map = Properties.Resources.Map1;
            }
            else
            {
                map = Properties.Resources.Map2;
            }
            return map;
        }
        private void FormMain_Load(object sender, EventArgs e)
        {
            //初始化
            cbMap.SelectedIndex = 0;//ComboBox控件设置默认显示项
        }
        private void Game_gameOver()
        {
            if (!game.isShowGameOver)
            {
                game.isShowGameOver = true;
                //播放死亡音效
                SoundPlayer soundPlayer = new SoundPlayer(Properties.Resources.死亡);
                soundPlayer.Play();
                //显示游戏结束，定义窗体dialoresult属性
                DialogResult dialogResult = MessageBox.Show("Game Over");
                if (dialogResult == DialogResult.OK)//用户点击确定按钮
                {
                    //重置游戏
                    game = new Game(GetMap());//初始化地图选择
                    game.InitGame(panelLife.Controls);//初始化生命值
                    game.gameChanged += Game_gameChanged;
                    game.gameOver += Game_gameOver;
                    gameArea.Invalidate();
                }
            }
        }
        private void gameArea_Paint(object sender, PaintEventArgs e)
        {
            if (game != null)
            {
                //绘制游戏区域
                game.Draw(e.Graphics, this.gameArea.Size);
            }
        }


        private void gameArea_Load(object sender, EventArgs e)
        {
            //初始化
            game = new Game(GetMap());//初始化地图选择
            game.InitGame(panelLife.Controls);//初始化生命值
            game.gameChanged += Game_gameChanged;
            game.gameOver += Game_gameOver;
        }

        private void Game_gameChanged()
        {
            if (game != null)
            {
                //重绘游戏区域
                gameArea.Invalidate();
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (game != null)
            {
                //按键传给game游戏初始化
                game.KeyDown(e.KeyCode.ToString());
                gameArea.Invalidate();
            }
        }

        private void CbMap_SelectedIndexChanged(object sender, EventArgs e)
        {
            //游戏初始化
            game.InitGame(panelLife.Controls);//初始化生命值
            game.gameChanged += Game_gameChanged;
            game.gameOver += Game_gameOver;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void btnAboutMe_Click(object sender, EventArgs e)
        {
            //关于我界面打开
            FrmAboutMe frm = new FrmAboutMe();
            frm.Show();
        }

        private void btnStartGame_Click(object sender, EventArgs e)
        {
            //重置游戏
            game = new Game(GetMap());//初始化地图选择
            game.InitGame(panelLife.Controls);//初始化生命值
            game.gameChanged += Game_gameChanged;
            game.gameOver += Game_gameOver;
            gameArea.Invalidate();

        }
    }
}
